<!--
 * @Descripttion:
 * @version:
 * @Author: WangMingJun
 * @Date: 2023-03-18 13:25:54
 * @LastEditors: WangMingJun
 * @LastEditTime: 2023-03-19 14:12:13
-->
<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">起始地点</label>
        <el-input v-model="query.fromCity" clearable placeholder="起始地点" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">终点</label>
        <el-input v-model="query.toCity" clearable placeholder="终点" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">起始时间</label>
        <el-time-picker
          v-model="query.startTime"
          placeholder="起始时间"
        />
        <!-- <el-input v-model="query.startTime" clearable placeholder="起始时间" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <label class="el-form-item-label">到达时间</label>
        <el-time-picker
          v-model="query.endTime"
          placeholder="到达时间"
        />
        <!-- <el-input v-model="query.endTime" clearable placeholder="到达时间" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="50%">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="起始地点" prop="fromCity">
            <el-input v-model="form.fromCity" />
          </el-form-item>
          <el-form-item label="终点" prop="toCity">
            <el-input v-model="form.toCity" />
          </el-form-item>
          <el-form-item label="起始时间" prop="startTime">
            <!-- <el-date-picker v-model="form.startTime" type="datetime" />
             -->
            <el-time-picker
              v-model="form.startTime"
              placeholder="起始时间"
            />
          </el-form-item>
          <el-form-item label="到达时间" prop="arriveTime">
            <!-- <el-date-picker v-model="form.arriveTime" type="datetime" /> -->
            <el-time-picker
              v-model="form.arriveTime"
              placeholder="起始时间"
            /></el-form-item>
          </el-form-item>
          <el-form-item label="车次" prop="trainNum">
            <el-input v-model="form.trainNum" />
          </el-form-item>
          <el-form-item label="价格" prop="price">
            <el-input v-model="form.price" />
          </el-form-item>
          <el-form-item label="座位数" prop="number">
            <el-input v-model="form.number" />
          </el-form-item>
          <el-form-item label="车厢数" prop="trainCars">
            <el-input v-model="form.trainCars" />
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="trainNum" label="车次" />

        <el-table-column prop="fromCity" label="起始地点" />
        <el-table-column prop="toCity" label="终点" />
        <el-table-column prop="startTime" label="起始时间" />
        <el-table-column prop="arriveTime" label="到达时间" />
        <el-table-column prop="price" label="价格" />
        <el-table-column prop="number" label="座位数" />
        <el-table-column prop="trainCars" label="车厢数" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="modifyTime" label="修改时间" />
        <el-table-column v-if="checkPer(['admin','train:edit','train:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudTrain from '@/api/train'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import moment from 'moment'

const defaultForm = { id: null, fromCity: null, toCity: null, startTime: null, trainNum: null, price: null, number: null, createTime: null, modifyTime: null, arriveTime: null, trainCars: null }
export default {
  name: 'Train',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '火车票', url: 'api/train', idField: 'id', sort: 'id,desc', crudMethod: { ...crudTrain }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'train:add'],
        edit: ['admin', 'train:edit'],
        del: ['admin', 'train:del']
      },
      rules: {
        fromCity: [
          { required: true, message: '起始地点不能为空', trigger: 'blur' }
        ],
        toCity: [
          { required: true, message: '终点不能为空', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '起始时间不能为空', trigger: 'blur' }
        ],
        trainNum: [
          { required: true, message: '车次不能为空', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '价格不能为空', trigger: 'blur' }
        ],
        number: [
          { required: true, message: '座位数不能为空', trigger: 'blur' }
        ],
        arriveTime: [
          { required: true, message: '到达时间不能为空', trigger: 'blur' }
        ],
        trainCars: [
          { required: true, message: '车厢数不能为空', trigger: 'blur' }
        ]

      },
      queryTypeOptions: [
        { key: 'fromCity', display_name: '起始地点' },
        { key: 'toCity', display_name: '终点' },
        { key: 'startTime', display_name: '起始时间' },
        { key: 'endTime', display_name: '到达时间' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeSubmit](form) {
      if (this.crud.status.add) {
        const curTime = moment().format('YYYY-MM-DD HH:mm:ss')
        form.form.createTime = curTime
      }
      if (this.crud.status.edit) {
        const curTime = moment().format('YYYY-MM-DD HH:mm:ss')
        form.form.modifyTime = curTime
      }
    }
  }
}
</script>

<style  lang="scss" scoped>
	.el-form {
		display: flex;
		flex-wrap: wrap;
		.el-form-item {
			width: 49%;
			.el-date-editor {
				width: 100%;
			}
			.el-select {
				width: 100%;
			}
			::v-deep .el-input-number {
				width: 100%;
				.el-input {
					.el-input__inner {
						text-align: left !important;
					}
				}
			}
		}
	}
</style>
